package com.cynteka.cyfoman.ui;

import com.cynteka.cyfoman.core.Util;
import com.cynteka.cyfoman.core.ValueObject;
import com.cynteka.cyfoman.core.config.ActionMD;
import com.cynteka.cyfoman.core.config.ActionsMD;
import com.cynteka.cyfoman.core.config.JAXBLoader;

import javax.sql.DataSource;
import javax.xml.bind.JAXBException;
import java.awt.*;
import java.io.IOException;

/**
 * User: Sergey
 * Date: 03.08.11
 * Time: 14:33
 */
public class EditListener implements IEventListener {

    private JAXBLoader jaxbLoader = new JAXBLoader();
    private String fileName;
    private ActionsMD actionsMD;
    private DataSource dataSource;
    private EventDispatcher eventDispatcher;

    public EventDispatcher getEventDispatcher() {
        return eventDispatcher;
    }

    public void setEventDispatcher(EventDispatcher eventDispatcher) {
        this.eventDispatcher = eventDispatcher;
    }

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Override
    public void handleEvent(IEvent event) {
        if (event instanceof EditEvent) {
            EditEvent editEvent = (EditEvent) event;
            String entityName = editEvent.getEntityName();
            Object entityId = editEvent.getEntityId();
            initAction();

            final ActionMD action = getAction(entityName);
            if (action != null) {
                final ValueObject valueObject = Util.populateObject(action, entityId, dataSource);

                ActionParamDlg paramDlg = new ActionParamDlg(action, valueObject) {
                    @Override
                    protected void onOK() {
                        super.onOK();
                        // ActionProcessor.processAction(action, valueObject, baseFolder, dataSource, filesToCopy);
                        valueObject.firePrepareToBeStored();
                        eventDispatcher.sendEvent(new StoreEvent(valueObject, action));
                        eventDispatcher.sendEvent(new UIEvent(UIEvent.REFRESH));
                    }
                };
                paramDlg.pack();
                Dimension size = paramDlg.getSize();
                paramDlg.setSize(size.width + 50, size.height);
                paramDlg.setLocationRelativeTo(null);

                paramDlg.setVisible(true);

            } else {

            }
        }
    }

    protected ActionMD getAction(String entityName) {
        for (ActionMD action : actionsMD) {
            if (entityName.equals(action.getTable())) {
                return action;
            }
        }
        return null;
    }

    protected void initAction() {
        try {
            actionsMD = jaxbLoader.loadXML(ActionsMD.class, fileName);

        } catch (JAXBException e) {
            // TODO redesign it
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (IOException e) {
            // TODO redesign it
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }
}
